///
//  LoadImage v 1.0
//  Russell Lowke, March 1st 2011
//
//  Copyright (c) 2011 Lowke Media
//  see http://www.lowkemedia.com for more information
//
//  Permission is hereby granted, free of charge, to any person obtaining a 
//  copy of this software and associated documentation files (the "Software"), 
//  to deal in the Software without restriction, including without limitation 
//  the rights to use, copy, modify, merge, publish, distribute, sublicense, 
//  and/or sell copies of the Software, and to permit persons to whom the 
//  Software is furnished to do so, subject to the following conditions:
// 
//  The above copyright notice and this permission notice shall be included in 
//  all copies or substantial portions of the Software.
// 
//  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 
//  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 
//  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 
//  AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 
//  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 
//  FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS 
//  IN THE SOFTWARE. 
//
//

function LoadImage(url)                     // (string) url of file being loaded
{   
    this.image;                             // (Image) Image object currently loading
    
    this.startLoad = function()
    {
        this.image = new Image();
        
        var obj = this;
        $(this.image)
            .load(this.url, function () { obj.verify(); })
            .error(this.url, function () { obj.loadError(); });
        
        // start loading image
        this.image.src = this.url;
    }
    
    this.loadError = function() // CHECK EVT FOR EXACT ERROR MESSAGE!
    {
        this.loadFailed("Failed to load image:" + this.url, 0);
    }
    
    this.verify = function()
    {
        // although loaded it still takes time for image to register
        if (! LoadImage.IsImageOK(this.image)) {
            var obj = this;
            setTimeout(function () { obj.verify() }, 100 ); // check every 1/10 of a second
        } else {
            this.fileLoaded();
        }
    }
    
    this.fileLoaded = function() 
    {
        LogAs.debug("File loaded (" + this.id + "): " + this.url, AssetLoader.DEBUG_FILE_LOADED);
        var evt = $.Event(LoadBase.ASSET_LOADED_EVENT);
        evt.asset = this.image;
        $(this).trigger(evt);
        this.assetLoader.removeAssetLoader(this);
    }
    
    this.cleanup = function()
    {
        this.image = null;
    }
    
    this.toString = function() {
        return "[LoadImage (url:" + this.url + ", id:" + this.id + ")]";
    }
    
    this.initialize(url);
};


LoadImage.IsImageOK = function(img) {

    if (! img || ! img.complete) {
        return false;
    }
    
    if (img.width === 0 && img.height === 0) {
        return false;
    }
    
    return true;
}

LoadImage.prototype = new LoadBase();                   // LoadImage class extends LoadBase

